// import { Tree } from 'antd';
// import { DownOutlined , MehOutlined} from '@ant-design/icons';
// import type { DataNode } from 'antd/es/tree';
// import React, { useMemo, useState } from 'react';
// import './index.less'

// const defaultData: DataNode[] = [
//   {
//     title: '联通软件研究院',
//     key: '0-0',
//     children: [
//       {
//         title: '联通软件研究院本部',
//         key: '0-0-0',
//         children: [
//           { title: '架构部', key: '0-0-0-0' },
//         ],
//       },
//       {
//         title: '济南软件研究院',
//         key: '0-0-1',
//       },
//     ],
//   },
//   {
//     title: '哈尔滨软件研究院',
//     key: '0-1',
//     children: [
//       { title: '管理层', key: '0-1-0-0' },
//       { title: '办公室', key: '0-1-0-1' },
//       { title: '财务部', key: '0-1-0-2' },
//     ],
//   },
//   {
//     title: '天津软件研究院',
//     key: '0-2',
//   },
// ];

// const dataList: { key: React.Key; title: any; }[] = [];
// const generateList = (data: DataNode[]) => {
//   for (let i = 0; i < data.length; i++) {
//     const node = data[i];
//     const { key ,title } = node;
//     dataList.push({ key, title });
//     if (node.children) {
//       generateList(node.children);
//     }
//   }
// };
// generateList(defaultData);

// const getParentKey = (key: React.Key, tree: DataNode[]): React.Key => {
//   let parentKey: React.Key;
//   for (let i = 0; i < tree.length; i++) {
//     const node = tree[i];
//     if (node.children) {
//       if (node.children.some((item) => item.key === key)) {
//         parentKey = node.key;
//       } else if (getParentKey(key, node.children)) {
//         parentKey = getParentKey(key, node.children);
//       }
//     }
//   }
//   return parentKey!;
// };

// const treeDemo: React.FC = () => {
//   const [expandedKeys, setExpandedKeys] = useState<React.Key[]>([]);
//   const [searchValue, setSearchValue] = useState('');
//   const [autoExpandParent, setAutoExpandParent] = useState(true);

//   const onExpand = (newExpandedKeys: React.Key[]) => {
//     setExpandedKeys(newExpandedKeys);
//     setAutoExpandParent(false);
//   };

//   const treeData = useMemo(() => {
//     console.log(searchValue,99999)
//     const loop = (data: DataNode[]): DataNode[] =>
//       data.map((item) => {
//         console.log(item,88888)
//         const strTitle = item.title as any;
//         const index = strTitle.indexOf(searchValue);
//         const beforeStr = strTitle.substring(0, index);
//         const afterStr = strTitle.slice(index + searchValue.length);
//         const title =
//           index > -1 ? (
//             <span>
//               {beforeStr}
//               <span className="site-tree-search-value" >{searchValue}</span>
//               {afterStr}
//             </span>
//           ) : (
//             <span>{strTitle}</span>
//           );
//         if (item.children) {
//           return { title, key: item.key, children: loop(item.children) };
//         }

//         return {
//           title,
//           key: item.key,
//         };
//       });

//     return loop(defaultData);
//   }, [searchValue]);

//   return (
//     <div>
//       <Tree.DirectoryTree
//         onExpand={onExpand}
//         expandedKeys={expandedKeys}
//         autoExpandParent={autoExpandParent}
//         treeData={treeData}
//         showIcon={true}
//         switcherIcon={<DownOutlined />}
//       />
//     </div>
//   );
// };

// export default treeDemo;

import { Tree } from 'antd';
import { DownOutlined, MehOutlined } from '@ant-design/icons';
import type { DataNode } from 'antd/es/tree';
import React, { useMemo, useState } from 'react';

interface DataNode {
  title: string;
  key: string;
  isLeaf?: boolean;
  children?: DataNode[];
}

const initTreeData: DataNode[] = [
  // { title: 'Expand to load', key: '0' },
  // { title: 'Expand to load', key: '1' },
  // { title: 'Tree Node', key: '2', isLeaf: true },
  {
    id: '001000',
    key: '001000',
    title: '中国联通总部',
    type: '0',
    children: null,
  },
  {
    id: '0011',
    key: '0011',
    title: '北京市分公司',
    type: '0',
    children: null,
  },
  {
    id: '0068',
    key: '0068',
    title: '中国联合网络通信股份有限公司(A股公司)',
    type: '0',
    children: null,
  },
  {
    id: '0071',
    key: '0071',
    title: '联通资产运营有限公司',
    type: '0',
    children: null,
  },
  {
    id: '0012',
    key: '0012',
    title: '天津市分公司',
    type: '0',
    children: null,
  },
  {
    id: '0059',
    key: '0059',
    title: '中国联合网络通信(香港)股份有限公司',
    type: '0',
    children: null,
  },
  {
    id: '0067',
    key: '0067',
    title: '联通华盛通信有限公司',
    type: '0',
    children: null,
  },
  {
    id: '0070',
    key: '0070',
    title: '中讯邮电咨询设计院有限公司',
    type: '0',
    children: null,
  },
  {
    id: '0013',
    key: '0013',
    title: '河北省分公司',
    type: '0',
    children: null,
  },
  {
    id: '0066',
    key: '0066',
    title: '联通信息导航有限公司',
    type: '0',
    children: null,
  },
  {
    id: '0083',
    key: '0083',
    title: '中国联通研究院',
    type: '0',
    children: null,
  },
  {
    id: '0085',
    key: '0085',
    title: '中国联通国际有限公司',
    type: '0',
    children: null,
  },
  {
    id: '0014',
    key: '0014',
    title: '山西省分公司',
    type: '0',
    children: null,
  },
  {
    id: '0015',
    key: '0015',
    title: '内蒙古自治区分公司',
    type: '0',
    children: null,
  },
  {
    id: '0077',
    key: '0077',
    title: '联通时科（北京）信息技术有限公司',
    type: '0',
    children: null,
  },
  {
    id: '0078',
    key: '0078',
    title: '联通在线信息科技有限公司',
    type: '0',
    children: null,
  },
  {
    id: '0018',
    key: '0018',
    title: '联通数字科技有限公司',
    type: '0',
    children: null,
  },
  {
    id: '0021',
    key: '0021',
    title: '辽宁省分公司',
    type: '0',
    children: null,
  },
  {
    id: '0075',
    key: '0075',
    title: '联通智慧安全科技有限公司',
    type: '0',
    children: null,
  },
  {
    id: '0022',
    key: '0022',
    title: '吉林省分公司',
    type: '0',
    children: null,
  },
  {
    id: '0069',
    key: '0069',
    title: '联通系统集成有限公司',
    type: '0',
    children: null,
  },
  {
    id: '0023',
    key: '0023',
    title: '黑龙江省分公司',
    type: '0',
    children: null,
  },
  {
    id: '0089',
    key: '0089',
    title: '联通支付有限公司',
    type: '0',
    children: null,
  },
  {
    id: '0091',
    key: '0091',
    title: '联通通信建设有限公司',
    type: '0',
    children: null,
  },
  {
    id: '0037',
    key: '0037',
    title: '山东省分公司',
    type: '0',
    children: null,
  },
  {
    id: '0094',
    key: '0094',
    title: '联通进出口有限公司',
    type: '0',
    children: null,
  },
  {
    id: '0041',
    key: '0041',
    title: '河南省分公司',
    type: '0',
    children: null,
  },
  {
    id: '0090',
    key: '0090',
    title: '京都信苑饭店',
    type: '0',
    children: null,
  },
  {
    id: '0031',
    key: '0031',
    title: '上海市分公司',
    type: '0',
    children: null,
  },
  {
    id: '0096',
    key: '0096',
    title: '联通网络技术研究院',
    type: '0',
    children: null,
  },
  {
    id: '0032',
    key: '0032',
    title: '江苏省分公司',
    type: '0',
    children: null,
  },
  {
    id: '0095',
    key: '0095',
    title: '联通云数据有限公司',
    type: '0',
    children: null,
  },
  {
    id: '0033',
    key: '0033',
    title: '浙江省分公司',
    type: '0',
    children: null,
  },
  {
    id: '0099',
    key: '0099',
    title: '联通创新创业投资有限公司',
    type: '0',
    children: null,
  },
  {
    id: '0034',
    key: '0034',
    title: '安徽省分公司',
    type: '0',
    children: null,
  },
  {
    id: '0055',
    key: '0055',
    title: '小沃科技有限公司',
    type: '0',
    children: null,
  },
  {
    id: '0056',
    key: '0056',
    title: '联通软件研究院',
    type: '0',
    children: null,
  },
  {
    id: '0035',
    key: '0035',
    title: '福建省分公司',
    type: '0',
    children: null,
  },
  {
    id: '0036',
    key: '0036',
    title: '江西省分公司',
    type: '0',
    children: null,
  },
  {
    id: '0058',
    key: '0058',
    title: '联通集团财务有限公司',
    type: '0',
    children: null,
  },
  {
    id: '0042',
    key: '0042',
    title: '湖北省分公司',
    type: '0',
    children: null,
  },
  {
    id: '0043',
    key: '0043',
    title: '湖南省分公司',
    type: '0',
    children: null,
  },
  {
    id: '0047',
    key: '0047',
    title: '中国联通物联网研究院',
    type: '0',
    children: null,
  },
  {
    id: '0044',
    key: '0044',
    title: '广东省分公司',
    type: '0',
    children: null,
  },
  {
    id: '0049',
    key: '0049',
    title: '联通大数据有限公司',
    type: '0',
    children: null,
  },
  {
    id: '0045',
    key: '0045',
    title: '广西壮族自治区分公司',
    type: '0',
    children: null,
  },
  {
    id: '0046',
    key: '0046',
    title: '海南省分公司',
    type: '0',
    children: null,
  },
  {
    id: '0050',
    key: '0050',
    title: '重庆市分公司',
    type: '0',
    children: null,
  },
  {
    id: '0051',
    key: '0051',
    title: '四川省分公司',
    type: '0',
    children: null,
  },
  {
    id: '0052',
    key: '0052',
    title: '贵州省分公司',
    type: '0',
    children: null,
  },
  {
    id: '0053',
    key: '0053',
    title: '云南省分公司',
    type: '0',
    children: null,
  },
  {
    id: '0054',
    key: '0054',
    title: '西藏自治区分公司',
    type: '0',
    children: null,
  },
  {
    id: '0061',
    key: '0061',
    title: '陕西省分公司',
    type: '0',
    children: null,
  },
  {
    id: '0080',
    key: '0080',
    title: '联通兴业通信技术有限公司',
    type: '0',
    children: null,
  },
  {
    id: '0062',
    key: '0062',
    title: '甘肃省分公司',
    type: '0',
    children: null,
  },
  {
    id: '0063',
    key: '0063',
    title: '青海省分公司',
    type: '0',
    children: null,
  },
  {
    id: '0064',
    key: '0064',
    title: '宁夏回族自治区分公司',
    type: '0',
    children: null,
  },
  {
    id: '0065',
    key: '0065',
    title: '新疆维吾尔自治区分公司',
    type: '0',
    children: null,
  },
  {
    id: '0057',
    key: '0057',
    title: '联通智网科技股份有限公司',
    type: '0',
    children: null,
  },
  {
    id: '0072',
    key: '0072',
    title: '临港数智科技（上海）有限公司',
    type: '0',
    children: null,
  },
  {
    id: '0074',
    key: '0074',
    title: '联通党校',
    type: '0',
    children: null,
  },
  {
    id: '0079',
    key: '0079',
    title: '联通智慧科技产业有限公司',
    type: '0',
    children: null,
  },
  {
    id: '00A20160670',
    key: '00A20160670',
    title: '临港数智科技（上海）有限公司',
    type: '0',
    children: null,
  },
  {
    id: '0029',
    key: '0029',
    title: '联通融资租赁有限公司',
    type: '0',
    children: null,
  },
  {
    id: '0048',
    key: '0048',
    title: '联通视频科技有限公司',
    type: '0',
    children: null,
  },
  {
    id: '0076',
    key: '0076',
    title: '联通智网创新中心',
    type: '0',
    children: null,
  },
  {
    id: '0081',
    key: '0081',
    title: '联通智能城市研究院',
    type: '0',
    children: null,
  },
  {
    id: '0093',
    key: '0093',
    title: '联通物联网有限责任公司',
    type: '0',
    children: null,
  },
  {
    id: '10206506',
    key: '10206506',
    title: '刘雁敏',
    type: '1',
    children: null,
  },
  {
    id: '10403839',
    key: '10403839',
    title: '蒋大春',
    type: '1',
    children: null,
  },
  {
    id: '10403838',
    key: '10403838',
    title: '危小其',
    type: '1',
    children: null,
  },
  {
    id: '10050773',
    key: '10050773',
    title: '陈静平',
    type: '1',
    children: null,
  },
  {
    id: '10386463',
    key: '10386463',
    title: '宋伟',
    type: '1',
    children: null,
  },
  {
    id: '10207302',
    key: '10207302',
    title: '王俏也',
    type: '1',
    children: null,
  },
  {
    id: '10261542',
    key: '10261542',
    title: '羡瑞栋',
    type: '1',
    children: null,
  },
  {
    id: '10327672',
    key: '10327672',
    title: '刘荣',
    type: '1',
    children: null,
  },
  {
    id: '10259420',
    key: '10259420',
    title: '徐孔融',
    type: '1',
    children: null,
  },
  {
    id: '10234966',
    key: '10234966',
    title: '刘建伟',
    type: '1',
    children: null,
  },
  {
    id: '10246622',
    key: '10246622',
    title: '王刚',
    type: '1',
    children: null,
  },
  {
    id: '10243392',
    key: '10243392',
    title: '李艺',
    type: '1',
    children: null,
  },
  {
    id: '10199562',
    key: '10199562',
    title: '祁贵宝',
    type: '1',
    children: null,
  },
  {
    id: '10271587',
    key: '10271587',
    title: '陈涛',
    type: '1',
    children: null,
  },
  {
    id: '10435840',
    key: '10435840',
    title: '何玉龙',
    type: '1',
    children: null,
  },
  {
    id: '20109016',
    key: '20109016',
    title: '杨蓬勃',
    type: '1',
    children: null,
  },
  {
    id: '10439362',
    key: '10439362',
    title: '谢杨',
    type: '1',
    children: null,
  },
  {
    id: '10299953',
    key: '10299953',
    title: '陈超',
    type: '1',
    children: null,
  },
  {
    id: '10292988',
    key: '10292988',
    title: '杨胡祥',
    type: '1',
    children: null,
  },
  {
    id: '10064392',
    key: '10064392',
    title: '李常青',
    type: '1',
    children: null,
  },
  {
    id: '10050107',
    key: '10050107',
    title: '温暖',
    type: '1',
    children: null,
  },
  {
    id: '10376644',
    key: '10376644',
    title: '周立',
    type: '1',
    children: null,
  },
  {
    id: '10438149',
    key: '10438149',
    title: '毕成',
    type: '1',
    children: null,
  },
  {
    id: '20056958',
    key: '20056958',
    title: '罗煦飞',
    type: '1',
    children: null,
  },
  {
    id: '10447798',
    key: '10447798',
    title: '王昕静',
    type: '1',
    children: null,
  },
  {
    id: '10447797',
    key: '10447797',
    title: '汪彩娥',
    type: '1',
    children: null,
  },
  {
    id: '10266993',
    key: '10266993',
    title: '周鑫',
    type: '1',
    children: null,
  },
  {
    id: '10280575',
    key: '10280575',
    title: '李建忠',
    type: '1',
    children: null,
  },
  {
    id: '20174070',
    key: '20174070',
    title: '罗丽娟',
    type: '1',
    children: null,
  },
  {
    id: '10414336',
    key: '10414336',
    title: '马涛',
    type: '1',
    children: null,
  },
  {
    id: '10386571',
    key: '10386571',
    title: '李楠',
    type: '1',
    children: null,
  },
  {
    id: '10282029',
    key: '10282029',
    title: '洪艳',
    type: '1',
    children: null,
  },
  {
    id: '10282183',
    key: '10282183',
    title: '王震科',
    type: '1',
    children: null,
  },
  {
    id: '10358667',
    key: '10358667',
    title: '张青伟',
    type: '1',
    children: null,
  },
  {
    id: '10381218',
    key: '10381218',
    title: '徐瑗霜',
    type: '1',
    children: null,
  },
  {
    id: '10280493',
    key: '10280493',
    title: '皇超奇',
    type: '1',
    children: null,
  },
  {
    id: '10584083',
    key: '10584083',
    title: '佘艳云',
    type: '1',
    children: null,
  },
  {
    id: '10583777',
    key: '10583777',
    title: '何小玲',
    type: '1',
    children: null,
  },
  {
    id: '10584082',
    key: '10584082',
    title: '章峻溯',
    type: '1',
    children: null,
  },
  {
    id: '10583778',
    key: '10583778',
    title: '陈雪',
    type: '1',
    children: null,
  },
  {
    id: '10061244',
    key: '10061244',
    title: '梁世红',
    type: '1',
    children: null,
  },
  {
    id: '10446539',
    key: '10446539',
    title: '陈畅',
    type: '1',
    children: null,
  },
  {
    id: '10630162',
    key: '10630162',
    title: '张晓庆',
    type: '1',
    children: null,
  },
  {
    id: '10605793',
    key: '10605793',
    title: '梁祥逸',
    type: '1',
    children: null,
  },
  {
    id: '10139403',
    key: '10139403',
    title: '李弟元',
    type: '1',
    children: null,
  },
  {
    id: '10443831',
    key: '10443831',
    title: '陈旭涛',
    type: '1',
    children: null,
  },
];

const updateTreeData = (
  list: DataNode[],
  key: React.Key,
  children: DataNode[],
): DataNode[] =>
  list.map((node) => {
    if (node.key === key) {
      return {
        ...node,
        children,
      };
    }
    if (node.children) {
      return {
        ...node,
        children: updateTreeData(node.children, key, children),
      };
    }
    return node;
  });

const treeDemo: React.FC = () => {
  const [treeData, setTreeData] = useState(initTreeData);

  const onLoadData = ({ key, children }: any) =>
    new Promise<void>((resolve) => {
      if (children) {
        resolve();
        return;
      }
      setTimeout(() => {
        setTreeData((origin) =>
          updateTreeData(origin, key, [
            { title: 'Child Node', key: `${key}-0` },
            { title: 'Child Node', key: `${key}-1` },
          ]),
        );

        resolve();
      }, 1000);
    });

  return (
    <div>
      <Tree loadData={onLoadData} treeData={treeData} />
    </div>
  );
};

export default treeDemo;
